<?php
class Model_Vote extends Joob_Db_Table{
	public $id;
	public $user_id;
	public $target_id, $type, $value, $since;
	
	protected $key = "id";
	
	protected $db = "votes";
	
	public function newVote($user_id,$target_id,$type,$value=0){
		$this->setOption('user_id',$user_id);
		$this->setOption('target_id',$target_id);
		$this->setOption('type',$type);
		$this->setOption('value',$value);
		$this->setOption('since',time());
	}

	public function checkVoted() {
        return $this->exist('user_id, target_id, type');
    }
	public static function checkVotedStatic($user_id,$target_id,$type) {
		$vote = new Model_Vote();
		$vote->newVote($user_id, $target_id, $type);
        return $vote->checkVoted();
    }
	public function addVote() {
        return $this->insert('user_id, target_id, type, value, since');
    }
    
	public function deleteByTarget() {
        $sql = "DELETE FROM votes WHERE target_id = '" . $this->target_id . "'";
        return SQL::update($sql);
    }
    
	public static function getByTarget($user_id, $type, $page,$page_size){
        $vote = new Model_Vote();
        return $vote->search("type='".$type."' and user_id=".$user_id, "*", $page*$page_size.','.$page_size);
    }
}